package top.humbleyuan.leet.datastructure.linkedlist;

/**
 * @Author YHH
 * @Date 2019/9/10 10:48
 * @Des
 */
public class LinkNode {

    public int val;

    public LinkNode next;

    public LinkNode() {}

    public LinkNode(int x) {val = x;}

    //生成链表
    public static LinkNode formLink(LinkNode head, int[] vals) {
        LinkNode l = head;
        for(int i = 0;i < vals.length;i++) {
            l.next = new LinkNode(vals[i]);
            l = l.next;
        }
        return head;
    }

    /**
     * 生成带环链表
     */
    public static LinkNode formLink(LinkNode head, int[] vals,int ringIndex) {
        if(ringIndex == -1)
            return formLink(head,vals);

        LinkNode l = head;
        LinkNode joinLink = l;
        for(int i = 0;i < vals.length;i++) {

            l.next = new LinkNode(vals[i]);
            if(i == ringIndex)
                joinLink = l;
            l = l.next;
        }
        l.next = joinLink;
        return head;
    }


    public static void headToString(LinkNode head) {

        String s = "";
        LinkNode f = head;
        for(; f!=null;) {
            s += f.val + " ";
            f = f.next;
        }
        System.out.println(s);
    }
}
